#include "game.h"
#include "IwGx.h"
#include "IwUI.h"

CIwUIElement* g_Screen;
CIwString<16> g_String;

class CController : public CIwUIController{
public:
	CController(){
		IW_UI_CREATE_VIEW_SLOT1(this, "CController", CController, OnPlayClick, CIwUIElement*);
		IW_UI_CREATE_VIEW_SLOT1(this, "CController", CController, OnEditorClick, CIwUIElement*);
		IW_UI_CREATE_VIEW_SLOT1(this, "CController", CController, OnSettingsClick, CIwUIElement*);

	}
private:
	void OnPlayClick(CIwUIElement*){

	}

	void OnEditorClick(CIwUIElement*){

	}

	void OnSettingsClick(CIwUIElement*){

	}
};


Game::Game(void)
{
}


Game::~Game(void)
{
}



void Game::Init()
{
	IwGxInit();
	IwUIInit();
	IwGxLightingOff();

	new CIwUIView;
	new CController;

	//Load the hello world UI
	IwGetResManager()->LoadGroup("layout.group");


	//Set the default style sheet
// 	CIwResource* pResource = IwGetResManager()->GetResNamed("iwui", IW_UI_RESTYPE_STYLESHEET);
// 	IwGetUIStyleManager()->SetStylesheet(IwSafeCast<CIwUIStylesheet*>(pResource));

	SetScreen("main");
}

void Game::Render()
{
	//Render the UI
	IwGetUIView()->Render();

//     // Allocate a material from the IwGx global cache
//     CIwMaterial* pMat = IW_GX_ALLOC_MATERIAL();
// 
//     // Set this as the active material
//     IwGxSetMaterial(pMat);
// 
//     // Set up screenspace vertex coords
//     int16 x1 = (int16)IwGxGetScreenWidth() * 1 / 4;
//     int16 x2 = (int16)IwGxGetScreenWidth() * 2 / 4;
//     int16 x3 = (int16)IwGxGetScreenWidth() * 3 / 4;
//     int16 y1 = (int16)IwGxGetScreenHeight() * 1 / 4;
//     int16 y3 = (int16)IwGxGetScreenHeight() * 3 / 4;
// 
//     static CIwSVec2 xy3[3];
//     xy3[0].x = x2, xy3[0].y = y1;
//     xy3[1].x = x1, xy3[1].y = y3;
//     xy3[2].x = x3, xy3[2].y = y3;
//     IwGxSetVertStreamScreenSpace(xy3, 3);
// 
//     // Set up vertex colours
//     static CIwColour cols[4] =
//     {
//         {0xff, 0x00, 0x00},
//         {0x00, 0xff, 0x00},
//         {0x00, 0x00, 0xff},
//     };
//     IwGxSetColStream(cols, 3);
// 
//     // Draw single triangle
//     IwGxDrawPrims(IW_GX_TRI_LIST, NULL, 3);
}


void Game::SetScreen(const char* screen)
{
	//Find the dialog template
	CIwUIElement* pDialogTemplate = (CIwUIElement*)IwGetResManager()->GetResNamed(screen, "CIwUIElement");

	if( !pDialogTemplate )
		return;

	//Remove the old screen
	if( g_Screen )
		IwGetUIView()->DestroyElements();

	//And instantiate it
	g_Screen = pDialogTemplate->Clone();
	IwGetUIView()->AddElement(g_Screen);
	IwGetUIView()->AddElementToLayout(g_Screen);
}


bool Game::Update()
{
	IwGetUIController()->Update();
	IwGetUIView()->Update(25);
	return true;
}

void Game::Shutdown()
{
	delete IwGetUIController();
	delete IwGetUIView();
	
	IwGxTerminate();
	IwUITerminate();
}




